<template>
  <el-dialog
    title="收货地址"
    :title="options.title"
    :visible.sync="options.visible"
    width="40%"
    top="10vh">
    <el-form :model="form" :rules="rules" ref="form">
      <div>{{test11}}</div>
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="来源文件" :label-width="formLabelWidth" prop="metaatext">
        <el-select v-model="form.metaatext" placeholder="请选择" style="width: 100%" @change="fromCha(form.metaatext)" >
          <el-option
              v-for="item in fromItem"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="来源描述字段"
        :label-width="formLabelWidth"
        prop="fromfieldstext">
        <el-select v-model="form.fromfieldstext" placeholder="请选择" style="width: 100%" :disabled=disabled1 @change="fromCha1(form.fromfieldstext)">
          <el-option
              v-for="item in fromItems"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
<!--        <el-input v-model="form.fromfields" autocomplete="off"></el-input>-->
      </el-form-item>
      <el-form-item label="目标文件" :label-width="formLabelWidth" prop="metabtext" >
        <el-select v-model="form.metabtext" placeholder="请选择" style="width: 100%" @change="toCha(form.metabtext)">
          <el-option
              v-for="item in toItem"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目标描述字段" :label-width="formLabelWidth" prop="tofieldstext">
        <el-select v-model="form.tofieldstext" placeholder="请选择" style="width: 100%" :disabled=disabled2  @change="toCha1(form.tofieldstext)">
          <el-option
              v-for="item in toItems"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签" :label-width="formLabelWidth" disabled="" prop="tagtext">
        <el-select v-model="form.tagtext" placeholder="请选择" style="width: 100%"  :disabled=disabled3  @change="tagCha(form.tagtext)" >
          <el-option
              v-for="item in tagItem"
              :key="item.id"
              :label="item.name"
              :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="算子class"
        :label-width="formLabelWidth"
        prop="transferoperatorclass">
        <el-select v-model="form.transferoperatorclass" placeholder="请选择" style="width: 100%"   :disabled=disabled3  @change="classCha(form.transferoperatorclass)">
          <el-option
              v-for="item in tableData"
              :key="item.id"
              :label="item.classname"
              :value="item.classname">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="转换算子方法"
        :label-width="formLabelWidth"
        prop="transferoperatorfunction">
        <el-input v-model="form.transferoperatorfunction"  disabled autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      options: {
        type: Object,
        default() {
          return {};
        },
      },
      row: {
        type: Object,
        default() {
          return null;
        },
      },
      ruleid: {
        type: Number,
        default() {
          return null;
        },
      },
    },
    data() {
      return {
        test11:"",
        tagItem: [
          { id: 2, name: "test1" },
          { id: 3, name: "test2" },
        ],
        form: {
          fromfields:null,
          tofields:null,
          transferoperatorfunction:"ceshi"
        },
        rules: {
          name: [{ required: true, message: "请输入名称", trigger: "blur" }],
          fromfieldstext: [
            { required: true, message: "请选择来源描述字段", trigger: "blur" },
          ],
          tofieldstext: [
            { required: true, message: "请选择目标描述字段", trigger: "blur" },
          ],
          transferoperatorclass: [
            { required: true, message: "请输入算子class", trigger: "blur" },
          ],
          transferoperatorfunction: [
            { required: true, message: "请输入转换算子方法", trigger: "blur" },
          ],

          // path: [
          //   { required: true, message: "请输入路径", trigger: "blur" },
          //   // {
          //   //   min: 1,
          //   //   max: 18,
          //   //   message: "长度在 1 到 18 个字符",
          //   //   trigger: "blur",
          //   // },
          // ],
          type: [
            {
              required: true,
              message: "请至少选择一个类型",
              trigger: "change",
            },
          ],
        },
        formLabelWidth: "120px",
        filePath: "",
        filetext: "",
        //来源文件
        fromItem:{},
        //目标文件
        toItem:{},
        //来源描述字段
        fromItems:{},
        //目标描述字段
        toItems:{},
        disabled1:true,
        disabled2:true,
        disabled3:true,
        //算子
        tableData:{},
        formtfiletype:'',
      };
    },
    created() {
      this.init();
      this.getMetaA();
      this.getMetaB();
      this.getTagAll();
    },
    methods: {
      init() {
        if (this.row) {
          this.form = JSON.parse(JSON.stringify(this.row));
          console.log("🚀 ~ init ~ this.form:", this.form);
        }
      },

      getMetaA(){
        let item = {
          "type":1
        };
        let itemJson = JSON.stringify(item);
        let result = window.iMeta.findMetaList(itemJson,1,100);
        let res = JSON.parse(result);
        this.fromItem = res.data.dataList;
      },
      getMetaB(){
        let item = {
          "type":2
        };
        let itemJson = JSON.stringify(item);
        let result = window.iMeta.findMetaList(itemJson,1,100);
        let res = JSON.parse(result);
        this.toItem = res.data.dataList;
      },
      //获取来源文件id查询来源描述字段
      fromCha(value){
        let result = window.iMetaItem.getAllMetafield(value);
        let res = JSON.parse(result);
        this.fromItems = res.data;
        if(res.code == 1){
          this.disabled1 = false;
        }
      },
      toCha(value){
        let result = window.iMetaItem.getAllMetafield(value);
        let res = JSON.parse(result);
        this.toItems = res.data;
        if(res.code == 1){
          this.disabled2 = false;
        }
      },
      //更新来源描述字段获取id
      fromCha1(value){
        if(typeof value === 'number'){
          this.form.fromfields = value;
          let i = 0;
          for (i = 0;i < this.fromItems.length;i++){
            if (value == this.fromItems[i].id){
              this.formtfiletype = this.fromItems[i].type;
            }
          }
        }
        this.disabled3=false
      },
      classCha(value){
        let i = 0;
        for (i = 0;i < this.tableData.length;i++){
          if (value == this.tableData[i].classname){
            this.form.transferoperatorfunction = this.tableData[i].function;
          }
        }
      },
      //更新目标描述字段获取id
      toCha1(value){
        if(typeof value === 'number'){
          this.form.tofields = value;
        }
      },
      //通过标签和来源字段类型获取算子
      tagCha(value){
        // this.test11 =value
        if(typeof value === 'number'){
          this.form.tag_id = value;
        }
        let item ={
          fromfieldtype:this.formtfiletype,
          tag_id:value,
        }
        let itemJson = JSON.stringify(item);
        let result = window.iOperator.findOperatorList(itemJson,1,100);
        let res = JSON.parse(result);
        this.tableData = res.data.dataList;
      },

      //查询所有描述文件字段
      getMetall(params) {
        let result = window.iMetaItem.getAllMetafield(params);
        let res = JSON.parse(result);
        this.gridData = res.data;
      },
      //查询所有标签
      getTagAll() {
        let result = window.iTag.getAll();
        let res = JSON.parse(result);
        this.tagItem = res.data;
      },
      selectFile() {
        this.$refs.fileInput.click();
      },
      handleFileUpload(event) {
        const file = event.target.files[0];
        if (!file) {
          alert("请选择一个文件");
          return;
        }
        this.filePath = file.name;
        console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        let result = window.iOperator.upload(this.filePath);
        let res = JSON.parse(result);
        this.filetext = res.data;
        this.form.path = res.data;
        // if (code == 1) {
        //   this.$message.success(res.msg);
        // } else {
        //   this.$message.error(res.msg);
        // }
        this.$refs.fileInput.value = null;

        // 检查文件类型是否为 JSON
        // const fileExtension = file.name.split(".").pop().toLowerCase();
        // if (fileExtension !== "json") {
        //   this.$message.error("只允许上传JSON格式的文件");
        //   return;
        // } else {
        //   this.filePath = file.name;
        //   this.form.path = 111;
        //   console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        //   // const fileName = file.name.split("\\").pop();
        //   // this.fileName = fileName.split(".")[0];
        //   // let item = {
        //   //   // key: this.fileName2,
        //   //   key: "MetaB",
        //   //   path: this.filePath2,
        //   // };
        //   // let itemAdd = JSON.stringify(item);
        //   // let result = window.iTest.upload(itemAdd);
        //   // let res = JSON.parse(result);
        //   // if (res.code == 1) {
        //   //   this.$message.success(res.msg);
        //   // } else {
        //   //   this.$message.error(res.msg);
        //   // }
        //   // this.$refs.fileInput2.value = null;
        // }
      },

      confirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.form.ruleid = this.ruleid;
            this.row
              ? this.$emit("onEditClose", this.form)
              : this.$emit("onAddClose", this.form);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      close() {
        this.row ? this.$emit("onEditClose") : this.$emit("onAddClose");
      },
    },
  };
</script>

<style scoped>
  :deep(.el-dialog) {
    padding-right: 30px;
    border-radius: 15px;
  }
</style>
